package org.simpledao.client;

import java.sql.SQLException;
import java.util.Map;
import java.util.HashMap;
import java.util.List;

import javax.sql.DataSource;

import org.mytool.cache.CacheExecuteI;
import org.mytool.common.db.TableInfoI;
import org.simpledao.cache.TableInfoCacheI;
import org.simpledao.service.SimpledaoI;
import org.simpledao.service.SimpledaoImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

/**
 * HashMap parameter: Same as field or effect of hump
 * pojo parameter:effect of hump
 * @author wangchen
 *
 */
public class SimpledaoHelper implements SimpledaoHelpI{

	private final String  ENTITY_TABLE_PREX = "SIMPLEDAO_ENTITY_TABLE_";
	
	private DataSource dbsource;
	
	private JdbcTemplate jdbcTemplate;
	
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
		return namedParameterJdbcTemplate;
	}

	


	private TableInfoI info;
	
	private TableInfoCacheI tableInfoCache;
	
	public TableInfoCacheI getTableInfoCache() {
		return tableInfoCache;
	}

	public void setTableInfoCache(TableInfoCacheI tableInfoCache) {
		this.tableInfoCache = tableInfoCache;
	}


	private String jdbcUrl;
	
	public DataSource getDbsource() {
		return dbsource;
	}

	public TableInfoI getInfo() {
		return info;
	}

	public void setInfo(TableInfoI info) {
		this.info = info;
	}

	public void setDbsource(DataSource dbsource) {
		this.dbsource = dbsource;
	}

	@Autowired
	private CacheExecuteI cacheExecute;

	private Map<String,SimpledaoI> cacheSimpleDaoMap = new HashMap<String,SimpledaoI>();


	public String getJdbcUrl() {
		return jdbcUrl;
	}

	public void setJdbcUrl(String jdbcUrl) {
		this.jdbcUrl = jdbcUrl;
	}

	public void init(){
		//info = new MysqlTableInfo();
		info.setDataSource(dbsource);
        info.setDbNameByUrl(jdbcUrl);
        tableInfoCache.setInfo(info);
        namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
	}
	
	public SimpledaoI  createCudOperation(String tableName){
//		SimpledaoI dao = (SimpledaoI)cacheExecute.get(ENTITY_TABLE_PREX+tableName);
		SimpledaoI dao = (SimpledaoI)cacheSimpleDaoMap.get(ENTITY_TABLE_PREX+tableName);
		if(dao==null){
			dao =  new SimpledaoImpl(tableInfoCache, tableName,this.jdbcTemplate,this.namedParameterJdbcTemplate);
//			cacheExecute.set(ENTITY_TABLE_PREX+tableName,dao);
			cacheSimpleDaoMap.put(ENTITY_TABLE_PREX+tableName,dao);
		}
		return  dao;
	}
	

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	@Override
	public void clearCacheTable(String tableName) {
		// TODO Auto-generated method stub
		tableInfoCache.clear(tableName);
	}
	
	@Override
	public void clearCacheTables(List<String> tableNames) {
		// TODO Auto-generated method stub
		if(tableNames==null ||tableNames.size()==0){
			return;
		}
		for(String tableName : tableNames){
			tableInfoCache.clear(tableName);
		}
		
	}

	@Override
	public void clearAllCacheTable() {
		// TODO Auto-generated method stub
		 tableInfoCache.clearAll();
	}

}
